package com.rh.bmw4s.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.adapter.IndexAdapter;
import com.rh.bmw4s.bean.BannerImgBean;
import com.rh.bmw4s.bean.IndexGridBean;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.ui.SearchActivity;
import com.rh.bmw4s.ui.WebShowActivity;
import com.rh.bmw4s.utills.BannerImageLoader;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.LogUtils;
import com.rh.bmw4s.utills.MyBanner;
import com.rh.bmw4s.utills.SPUtils;
import com.rh.bmw4s.utills.ToastUtils;
import com.tencent.bugly.beta.Beta;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/9/27.
 */

public class OneFragment extends Fragment {
    @BindView(R.id.ll_index_search)
    LinearLayout llSearch;
    @BindView(R.id.img_index_news)
    ImageView imgNews;
    Unbinder unbinder;
    private LRecyclerView mRecyclerView = null;
    private IndexAdapter mDataAdapter = null;
    private LRecyclerViewAdapter mLRecyclerViewAdapter = null;

    private MyBanner banner;
    private Gson gson;
    private int anInt;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_1, container, false);
        gson = new Gson();
        anInt = SPUtils.getInstance().getInt(ConfigValue.userId);

        //banner
        getBannerImgs(view);

        mRecyclerView = (LRecyclerView) view.findViewById(R.id.index_recy);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        mRecyclerView.setLayoutManager(manager);

        mDataAdapter = new IndexAdapter(getActivity(), anInt);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(mDataAdapter);
        mRecyclerView.setAdapter(mLRecyclerViewAdapter);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setArrowImageView(R.drawable.ic_pulltorefresh_arrow);

        //divider
        GridItemDecoration divider = new GridItemDecoration.Builder(getActivity())
                .setHorizontal(R.dimen.size_1dp)
                .setVertical(R.dimen.size_1dp)
                .setColorResource(R.color.app_bg1)
                .build();
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(divider);

        //禁止下拉刷新
        mRecyclerView.setPullRefreshEnabled(false);
        mRecyclerView.setLoadMoreEnabled(false);
        //添加脚部
        // mLRecyclerViewAdapter.addFooterView(new SampleFooter(getActivity()));

        //gridview
        getGridInfo();

        //检查更新
        Beta.checkUpgrade(false, false);

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void getGridInfo() {
        DialogUtils.showProgressDlg(getActivity(), "努力加载中......");
        OkGo.<String>post(Constant.IndexGrid)
                .tag(getActivity())
                .params("car_id", anInt)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        IndexGridBean gridBean = gson.fromJson(response.body().toString(), IndexGridBean.class);
                        if (gridBean.getStatus() == Constant.SUCESS) {
                            List<IndexGridBean.DatasBean> datas = gridBean.getDatas();
                            mDataAdapter.addAll(datas);
                            mRecyclerView.refreshComplete(datas.size());
                        } else {
                            ToastUtils.toastForShort(getActivity(), "获取信息失败");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(getActivity(), "获取信息失败");
                    }
                });

    }

    /**
     * 获取轮播图片
     */
    private void getBannerImgs(View view) {
        banner = (MyBanner) view.findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new BannerImageLoader());
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(1500);


        OkGo.<String>get(Constant.BannerInfo)
                .tag(getActivity())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        LogUtils.d("fff", response.body().toString());
                        BannerImgBean imgBean = gson.fromJson(response.body().toString(), BannerImgBean.class);
                        if (imgBean.getStatus() == Constant.SUCESS) {
                            List<String> imgDatas = new ArrayList<String>();
                            //设置图片集合
                            final List<BannerImgBean.DatasBean> datas = imgBean.getDatas();
                            for (int i = 0; i < datas.size(); i++) {
                                BannerImgBean.DatasBean datasBean = datas.get(i);
                                imgDatas.add(datasBean.getPicture());
                            }

                            banner.setOnBannerListener(new OnBannerListener() {
                                @Override
                                public void OnBannerClick(int position) {

                                    String ban_url = datas.get(position).getBan_url();
                                    if (!TextUtils.isEmpty(ban_url)) {
                                        Intent intent = new Intent(getActivity(), WebShowActivity.class);
                                        intent.putExtra("url", ban_url);
                                        getActivity().startActivity(intent);
                                    } else {
                                        //  ToastUtils.toastForShort(getActivity(), "位置" + position);
                                    }
                                }
                            });

                            banner.setImages(imgDatas);
                            banner.start();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        ToastUtils.toastForShort(getActivity(), "轮播图加载失败");

                    }
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.ll_index_search, R.id.img_index_news})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_index_search: //全局搜索
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                intent.putExtra("searchtype", 1);
                startActivity(intent);
                break;
            case R.id.img_index_news:
                Intent intent2 = new Intent(getActivity(), WebShowActivity.class);
                intent2.putExtra("url", Constant.H5NEWS + anInt);
                startActivity(intent2);
                break;
        }
    }
}
